package com.jwell.panprovider.ui.adapter

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panprovider.R
import com.jwell.panprovider.entity.WaybillListBean
import com.jwell.panprovider.tools.StringUtils
import com.jwell.panprovider.ui.activity.WaybillInfoActivity
import kotlinx.android.synthetic.main.item_transport_waybill_son_layout.view.*

/**
 * Created by 76577 on 2018/6/19.
 * 运单列表
 */
class WaybillListAdapter  constructor(list: List<WaybillListBean>) : RecyclerView.Adapter<WaybillListAdapter.ViewHolder>() {
    private var list = list


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_transport_waybill_son_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            text_contractNum.text = (list[position].mtrlOrderNo ?: "") + "(" + (list[position].materialName ?: "") + ")"
            text_son_waybill_code.text = list[position].forBillCode ?: ""
            text_son_dispatch_info.text = (list[position].driverName ?: "") + "/" + (list[position].carNum ?: "")
            text_son_state.text = list[position].appBillStateName ?: ""
            text_son_register_time.text = StringUtils.getInstance()
                    .setModal(StringUtils.TIME_TYPE_3)
                    .regularization(list[position].createDate, "T", " ")
            when (list[position].billState) {
                1, 4, 6, 7, 8 -> {//运输中,入库中,排号失效,超时未送达,申请延期
                    text_son_state_info.text = ""
                    text_son_state_info.visibility = View.GONE
                }
                2, 3 -> {//正在排号,缓冲区等待
                    text_son_state_info.text = "号数:" + (list[position].seQ_ID?:"") + "\n" + "等待人数:" + list[position].waitCount + "人"
                    text_son_state_info.visibility = View.VISIBLE
                }
                5 -> {//已收料
                    text_son_state_info.text = "净重:" + list[position].netWeight + "吨"
                    text_son_state_info.visibility = View.VISIBLE
                }
                9 -> {//已作废
                    text_son_state_info.visibility = View.VISIBLE
                    text_son_state_info.text = "(" + list[position].toVoidRemark + ")"
                }
                else -> {
                    text_son_state_info.text = ""
                    text_son_state_info.visibility = View.GONE
                }
            }
            holder.itemView.setOnClickListener {
                var intent = Intent(holder.itemView.context, WaybillInfoActivity::class.java)
                intent.putExtra("id", list[position].id)
                holder.itemView.context.startActivity(intent)
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}